package com.eshop.modules.activity.service.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import com.eshop.modules.product.vo.StoreProductQueryVo;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.eshop.modules.activity.domain.StoreBargain;
import com.eshop.common.mapper.CoreMapper;

@Repository
public interface StoreBargainMapper extends CoreMapper<StoreBargain>
{
    @Update({ "update store_bargain set stock=stock+#{num}, sales=sales-#{num} where id=#{bargainId}" })
    int incStockDecSales(@Param("num") final int num, @Param("bargainId") final Long bargainId);
    
    @Update({ "update store_bargain set stock=stock-#{num}, sales=sales+#{num} where id=#{bargainId} and stock >= #{num}" })
    int decStockIncSales(@Param("num") final int num, @Param("bargainId") final Long bargainId);
    
    @Select({ "SELECT c.id,c.image,c.min_price as price,c.price as otPrice,c.title as storeName,c.status as isShow,c.cost,c.is_postage as isPostage,c.postage,c.sales,c.stock,c.is_del as isDel FROM store_bargain c  WHERE c.id = #{id} and c.is_del = 0" })
    StoreProductQueryVo bargainInfo(final Long id);
    
    @Select({ "select IFNULL(sum(look),0)from store_bargain" })
    int lookCount();
    
    @Select({ "select IFNULL(sum(share),0) as shareCount from store_bargain" })
    int shareCount();
    
    @Update({ "update store_bargain set share=share+1 where id=#{id}" })
    void addBargainShare(@Param("id") final Long id);
    
    @Update({ "update store_bargain set look=look+1 where id=#{id}" })
    void addBargainLook(@Param("id") final Long id);
    
    @Delete({ "delete from system_attachment where name = #{name}" })
    void deleteBargainImg(@Param("name") final String name);
}
